package com.daocao.hua.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daocao.hua.R;
import com.daocao.hua.api.callback.ShareIndexContract;
import com.daocao.hua.api.requestInfo.ShareIndexRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.ShareIndexModel;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.UI;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.Dialog;
import com.lljjcoder.view.AlignTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
import butterknife.Unbinder;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.themes.OnekeyShare;

/**
 * Created by xsooy-pc on 2017/6/7.
 */

public class InviteGiftV2Activity extends BaseActivity implements ShareIndexContract.View {

    @BindView(R.id.img_bg)
    ImageView imgBg;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    private Unbinder unbiner;
    private ShareIndexRequest api;
    private String textCopy;
    private Spanned spanned;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_invite_v2_gift);
        unbiner = ButterKnife.bind(this);
        baseInitView(this, "InviteGiftActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        setBackIntent();
        setTitleText("分享有礼");
//        Glide.with(this).load(R.mipmap.img_invite_total_bg).into(imgBg);
//        UI.setTvHtml(txtTips, R.string.invite_tips, "#借款新口子推荐#今天发现一个借款新口子：现金卡。最高额1000元，首次下款最快只要5分钟，被拒还赔钱！！！扣子链接>>",
//                "www.baidu.com");
//        ShareSDK.initSDK(this);
        api = ShareIndexRequest.create();
        api.attachView(this);
        api.getShareIndex();
    }

    private String title;
    private String content;
    private String url;
    private String imageUrl;

    private void showShare() {
        if (TextUtils.isEmpty(title)) {
            return;
        }
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(content);
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(imageUrl);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment(content);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(title);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

// 启动分享GUI
        oks.show(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbiner.unbind();
        unbiner = null;
        api.detachView();
        api = null;
        spanned = null;
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void getShareIndexSuccess(ShareIndexModel item) {
        ShareIndexModel.Data.Share share = item.data.share;
        title = share.title;
        content = share.description;
        url = share.url;
        imageUrl = share.image;
        List<ShareIndexModel.Data.Rule> rules = item.data.rules;
        UI.setTvHtml(txtTips, R.string.invite_tips, item.data.text,
                url);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rules.size(); i++) {
            setSpanTitle(rules.get(i).title, sb);
            setSpanContent(rules.get(i).desc, sb);
        }
        spanned = Html.fromHtml(sb.toString());

    }

    public void setSpanTitle(String title, StringBuilder sb) {
        sb.append("<fon><big>");
        sb.append(title);
        sb.append("</big></font><br/>");
    }

    public void setSpanContent(String content, StringBuilder sb) {
        sb.append("<font color=\"#5B5F8E\">");
        sb.append(content);
        sb.append("</font><br/><br/>");
    }

    @OnClick({R.id.btn_rule, R.id.btn_invite, R.id.btn_reward})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_rule:
                if (TextUtils.isEmpty(spanned)) {
                    return;
                }
                Dialog.showInviteDialog(this, spanned).show();
                break;
            case R.id.btn_invite:
                if (TextUtils.isEmpty(title)) {
                    api.getShareIndex();
                    return;
                }
                showShare();
                break;
            case R.id.btn_reward:
                if (TextUtils.isEmpty(title)) {
                    api.getShareIndex();
                    return;
                }
                Intent intent = new Intent(this, InviteRecordActivity.class);
                intent.putExtra("title", title);
                intent.putExtra("content", content);
                intent.putExtra("url", url);
                intent.putExtra("imageUrl", imageUrl);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }

    @OnLongClick(R.id.txt_tips)
    public boolean onLongClick(View view) {
        textCopy = UI.toString(txtTips);
        if (TextUtils.isEmpty(textCopy)) {
            return false;
        }
        ClipboardManager c = (ClipboardManager)
                getSystemService(Context.CLIPBOARD_SERVICE);
        c.setText(textCopy);
        ToastUtil.makeText(this, "已成功复制到剪切板").show();
        return true;
    }
}
